<template>
  <div class="comment">
    <!-- 形态1. 简单的展示 -->
    <div
      class="type1"
      v-show="!isActive"
    >
      <input
        type="text"
        placeholder="写跟帖"
        @focus="inputFn"
      >
      <router-link :to="`/comment/${article.id}`" class="icon-group">
        <i class="iconfont iconpinglun-"></i>
        <em>{{article.comment_length}}</em>
      </router-link>
      <!-- 收藏按钮 -->
      <span
        class="icon-group"
        @click="collectFn"
      >
        <i
          class="iconfont iconshoucang"
          :class="{
          star: article.has_star
        }"
        ></i>
      </span>
      <span class="icon-group">
        <i class="iconfont iconfenxiang"></i>
      </span>
    </div>
    <!-- 形态2. 文本域，用户课室输入 -->
    <div
      class="type2"
      v-show="isActive"
    >
      <!-- Vant 也有组件 -->
      <textarea
        class="textarea"
        placeholder="回复: @火星网友"
       v-focus
       v-model.trim="content"
      ></textarea>
      <div class="btn-group">
        <span class="btn" @click="isActive=false">取消</span>
        <span class="btn" @click="sendBtn">发送</span>
      </div>
    </div>
  </div>
</template>

<script>
import { updatePostStarByIdAPI ,publishPostCommentAPI} from "../apis/news";
export default {
     props: {
    article: Object,
    commentObj: Object,
    
  },
  data() {
    return {
      isActive: false,
      content:"",
    };
  },
  watch: {
    commentObj:{
      deep:true,
      handler(){
        if(this.commentObj){
          //如果有被回复的评论，在文本域中添加@用户昵称
          this.content='@'+this.commentObj.user.nickname+''
        }
      }
    }
  },
  //监听回复里面的昵称是不是清空，清空了就返回null
  content(){
if(this.content===''){
  this.$emit('reset')
}
  },
   methods: {
    async collectFn() {
      // console.log("实现文章收藏", this.article.id);
      // 调用接口
      const res = await updatePostStarByIdAPI(this.article.id);
      // console.log(res);
      const { message } = res.data;
      // 提示操作状态
      if (message === "收藏成功") {
        this.$toast.success(message);
      } else {
        this.$toast.fail(message);
      }
      // 更新页面绑定的类名
      this.article.has_star = !this.article.has_star;
        // console.log('打印,',this.article);
    },
    inputFn(){
      this.isActive=true
      // // 需要等待视图更新后，才能获取到焦点
      // this.$nextTick(() => {
      //   this.$refs.textarea.focus();
      // });
      
    },
    //发送按钮
    sendBtn(){
      if(this.content==='') return this.$toast,fail('请输入内容')
      //调用发表评论的接口
 const res= publishPostCommentAPI(this.article.id,{
  content:this.content,
  parent_id:this.commentObj ?this.commentObj.id:null
})
this.$toast.success('发布成功')
//清空
this.content='';

//框隐藏
this.isActive=false;
// 由于评论列表的数据是在父组件上，子传父
      this.$emit("update");

    }
  },
};
</script>

<style lang="less" scoped>
// 定位的父盒子负责占位，定位的底部评论盖住了页面的内容
.comment_placeholder {
  height: 100px;
}
.comment_cover {
  // background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
}
.comment {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f2f2f2;
  z-index: 10;
}
.type1 {
  display: flex;
  height: 50px;
  align-items: center;
  padding: 0 10px;
  input {
    flex: 1;
    height: 30px;
    border-radius: 15px;
    background-color: #d7d7d7;
    border: none;
    padding: 0 10px;
    margin: 10px;
  }
  .icon-group {
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    .iconfont {
      font-size: 20px;
    }
    em {
      background-color: red;
      color: #fff;
      position: absolute;
      right: 0px;
      top: 0;
      font-size: 12px;
      border-radius: 15px;
      padding: 0 2px;
    }
  }
}
.type2 {
  display: flex;
  padding: 10px;
  .textarea {
    height: 80px;
    background-color: #d7d7d7;
    border-radius: 5px;
    flex: 1;
    padding: 5px;
  }

  .btn-group {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .btn {
      width: 60px;
      height: 25px;
      background-color: red;
      color: #fff;
      border-radius: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>